/*
 *Program Name: Bullet.cpp
 *Written By:   Cheng-Hsiu Hsieh
 */

#include "Bullet.h"

#include "SharedConstants.h"

#include "corona/corona.h"

#include <GL/glut.h>
#include <math.h>


cBullet::cBullet():cDrawable( 0.0f )
{
  setXPosition( 0.0f );
  setYPosition( 0.0f );
  setZPosition( 0.0f );
  mDamage = 0;
  mFired = false;
  mTimeElapsed = 0;

}

cBullet::cBullet( int iDamage ):cDrawable( 0.1f )
{
  setXPosition( 0.0f );
  setYPosition( 0.0f );
  setZPosition( 0.0f );
  mDamage = iDamage;
  mFired = false;
  mTimeElapsed = 0;
}

cBullet::~cBullet()
{
}

void cBullet::draw()
{
  if (  getZPosition() > kEnd ||
        getZPosition() < kStart )//too far
  {
    setHide( true );
    mFired = false;
  }
  
  if ( !getHide() && isFired() )
  {
    glPushMatrix();
    glTranslatef( getXPosition(), 
                  getYPosition(), 
                  getZPosition() );
    
    float fR = getRadius();
    glBegin(GL_QUADS);
    glColor3f( 0.0f, 0.0f, 0.0f );
    //Top
    glVertex3f( -fR, fR, -fR );
    glVertex3f( fR, fR, -fR );
    glVertex3f( fR, fR, fR );
    glVertex3f( -fR, fR, fR );

    //Bottom
    glVertex3f( -fR, -fR, fR );
    glVertex3f( fR, -fR, fR );
    glVertex3f( fR, -fR, -fR );
    glVertex3f( fR, -fR, -fR );

    //Side
    glVertex3f( -fR, fR, fR );
    glVertex3f( fR, fR, fR );
    glVertex3f( fR, -fR, fR );
    glVertex3f( -fR, -fR, fR );

    glVertex3f( fR, fR, -fR );
    glVertex3f( -fR, fR, -fR );
    glVertex3f( -fR, -fR, -fR );
    glVertex3f( fR, -fR, -fR );

    //Front
    glVertex3f( fR, fR, fR );
    glVertex3f( fR, fR, -fR );
    glVertex3f( fR, -fR, -fR );
    glVertex3f( fR, -fR, fR );

    //Back  
    glVertex3f( -fR, fR, -fR );
    glVertex3f( -fR, fR, fR );
    glVertex3f( -fR, -fR, fR );
    glVertex3f( -fR, -fR, -fR );

    glEnd();
    glPopMatrix();

    //Update the location ( moving forward )
    int duraTime = mTimeElapsed - GetTickCount();
    setZPosition( getZPosition() + 
                  mDirection * duraTime );
  }
}

void cBullet::die()
{
  setHide( true );
  mFired = false;
}

bool cBullet::reset( float fX, float fY, float fZ )
{
  bool bFlag = false;
  if ( !mFired )//not fired
  {
    setXPosition( fX );
    setYPosition( fY );
    setZPosition( fZ );
    setHide( false );
    bFlag = true;//If successfully reset    
  }
  return bFlag;
}

void cBullet::fire( float zVelocity )
{
  if ( !mFired )//not fired
  {
    mDirection = zVelocity;
    mTimeElapsed = GetTickCount();
    mFired = true;
  }
}

bool cBullet::isFired()
{
  return mFired;
}

